<?php


namespace app\Utils;


use app\admin\model\System\SysSettingModel;
use app\api\model\User\UserModel;

class CalculateUtil
{

    protected $setting;

    public function __construct() {
        $this->setting = SysSettingModel::getSysSettingWhere([['id','=',1]]);
    }

    public static function i()
    {
        $calculateUtil = new CalculateUtil();
        return $calculateUtil;
    }

    /**
     * 迭代上家用户
     * @param $level
     * @param $uid
     * @param $list
     * @param $start
     * @return mixed
     */
    public function getAgentList($level, $uid, $list, $start)
    {
        if ($start < $level) {
            if (!empty($uid)) {
                $start++;
                //查询上家
                $agent = UserModel::getUserWhere(['id' => $uid]);
                array_push($list, $agent);
                $list = $this->getAgentList($level, $agent['parent_uid'], $list, $start);
            }
        }
        return $list;
    }

    /**
     * 佣金计算
     * @param array $user 用户信息
     * @param array $goodsInfo 商品信息
     * @param array $order_goods_data 订单商品信息
     * @return mixed
     */
    public function commissionCalculate($user,$goodsInfo,$total_price,$order_goods_data)
    {
        $sales_setting = [];
        //全局配置
        $sys = $this->setting;
        if(!empty($sys['sys_is_sales'])) {
            if(!empty($goodsInfo['is_sales'])){
                if (!empty($goodsInfo['merchant_ratio']) && $goodsInfo['merchant_ratio'] > 0) {
                    if ($goodsInfo['is_sales'] == 1) {
                        //独立配置
                        $sales_setting = json_decode($goodsInfo['sales_setting'], true);
                        //平台抽成
                        $platform = bcmul($total_price, $goodsInfo['merchant_ratio'], 2);
                        //获取分销上家
                        $agent_data = [];
                        $agents = $this->getAgentList($sales_setting['level'], $user['parent_uid'], [], 0);
                        foreach ($agents as $k => $agent) {
                            //创业合伙人
                            if($agent['is_partner']){
                                if (!empty($sales_setting['level_sales' . $k]) && isset($sales_setting['level_sales' . $k])) {
                                    $sales_info = [];
                                    $sale       = $sales_setting['level_sales' . $k];
                                    //佣金
                                    $sale_price = bcmul($platform, bcdiv($sale, 100, 2), 2);
                                    if ($sale_price > 0) {
                                        $order_goods_data['agnet_uid' . ($k + 1)]    = $agent['id'];
                                        $order_goods_data['agent_amount' . ($k + 1)] = $sale_price;
                                        $sales_info['agnet_uid' . ($k + 1)]          = $agent['id'];
                                        $sales_info['agent_amount' . ($k + 1)]       = $sale_price;
                                        array_push($agent_data, $sales_info);
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                        $order_goods_data['agent_data'] = json_encode($agent_data);
                    } else {
                        if (!empty($sys['sys_sales_setting'])) {
                            $sales_setting = json_decode($sys['sys_sales_setting'], true);
                            //平台抽成
                            $platform = bcmul($total_price, $goodsInfo['merchant_ratio'], 2);
                            //获取分销上家
                            $agent_data = [];
                            $agents     = $this->getAgentList($sales_setting['level'], $user['parent_uid'], [], 0);
                            foreach ($agents as $k => $agent) {
                                //创业合伙人
                                if($agent['is_partner']){
                                    if (!empty($sales_setting['level_sales' . $k]) && isset($sales_setting['level_sales' . $k])) {
                                        $sales_info = [];
                                        $sale       = $sales_setting['level_sales' . $k];
                                        //佣金
                                        $sale_price = bcmul($platform, bcdiv($sale, 100, 2), 2);
                                        if ($sale_price > 0) {
                                            $order_goods_data['agnet_uid' . ($k + 1)]    = $agent['id'];
                                            $order_goods_data['agent_amount' . ($k + 1)] = $sale_price;
                                            $sales_info['agnet_uid' . ($k + 1)]          = $agent['id'];
                                            $sales_info['agent_amount' . ($k + 1)]       = $sale_price;
                                            array_push($agent_data, $sales_info);
                                        }
                                    } else {
                                        break;
                                    }
                                }
                            }
                            $order_goods_data['agent_data'] = json_encode($agent_data);
                        }
                    }
                }
            }
        }
        $order_goods_data['sales_data'] = json_encode($sales_setting);
        return $order_goods_data;
    }


}